package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 用户操作
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    //获得当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername() {
        //当Spring security完成认证后，会将当前用户信息保存到框架提供的上下文对象
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(user);
        if (user != null) {
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
        }

        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

    /*用户分页查询*/
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = userService.pageQuery(queryPageBean);
        return pageResult;
    }

    /*新增用户*/
    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user, Integer[] roleIds) {
        try {
            userService.add(user, roleIds);
            return new Result(true, "添加用户成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加用户失败");
        }
    }

    /*id查角色*/
    @RequestMapping("/findByUserId")
    public Result findByMenuId(Integer userId) {
        try {
            com.itheima.pojo.User user = userService.findByUserId(userId);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /*编辑*/
    @RequestMapping("/edit")
    public Result edit(@RequestBody com.itheima.pojo.User user, Integer[] roleIds) {
        try {
            userService.edit(user, roleIds);
            return new Result(true, "编辑用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑用户失败");
        }
    }

    /*delete*/
    @RequestMapping("/delete")
    public Result delete(Integer userId) {
        try {
            userService.delete(userId);
            return new Result(true, "删除用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除用户失败");
        }
    }

    /*findRoleIds*/
    @RequestMapping("/findRoleIds")
    public Result findRoleIds(Integer userId){
        try {
            Set<Integer> roleIds = userService.findRoleIds(userId);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("/getMenus")
    public Result getMenus(){
        Object user =  SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(user);
        if (user!=null){
            com.itheima.pojo.User user1 = userService.findByUserName(((User)user).getUsername());

            List<Map<String,Object>> maps = userService.findUserMenu(user1.getId());
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,maps);
        }
        return new Result(false,MessageConstant.GET_MENU_FAIL);

    }
}
